package com.tan.kotlintest.view

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Canvas
import android.os.Handler
import android.os.Message
import android.util.AttributeSet
import android.view.View
import com.tan.kotlintest.R

/*
 * Copyright (C) 2018 重庆一聊高科技有限公司
 * 版权所有
 *
 * 功能描述：加载中动画view
 * 作者：txj
 * 创建时间：2018/6/14
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
class LoadingView : View {
    private val MSG_ANIM = 100

    /**
     * 帧数量
     *
     * @param frameCount
     */
    var frameCount = 8  //该帧数是根据view的背景图片确定，不可随意变更
    /**
     * 每帧的时间间隔
     *
     * @param frameInterval
     */
    var frameInterval = 150 //每帧的时间差
    private var DURATION = frameCount * frameInterval

    private var mCurrFrame = 1

    private var mHandler: Handler? = null
    private var mCurrDegree: Int = 0
    private var mAnimInterval = 1
    private var mDeltaDegree = 1

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context, attrs)
    }

    @TargetApi(21)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        init(context, attrs)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        mCurrDegree = 0
    }

    public override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        if (mHandler != null) {
            mHandler!!.removeCallbacksAndMessages(null)
            mHandler = null
        }
    }

    override fun onDraw(canvas: Canvas) {
        canvas.rotate(mCurrDegree.toFloat())
        mCurrDegree = mCurrDegree % 360
        rotation = mCurrDegree.toFloat()
        if (mHandler == null) {
            createHandler()
        }
        mHandler!!.sendEmptyMessageDelayed(MSG_ANIM, mAnimInterval.toLong())
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        if (attrs != null) {
            val a = context.obtainStyledAttributes(attrs, R.styleable.LoadingView)
            frameCount = a.getInt(R.styleable.LoadingView_frame_count, frameCount)
            frameInterval = a.getInt(R.styleable.LoadingView_frame_interval, frameInterval)
            a.recycle()
        }

        createHandler()

        DURATION = frameCount * frameInterval
        mAnimInterval = DURATION / frameCount
        mDeltaDegree = 360 / frameCount
    }

    private fun createHandler() {
        mHandler = object : Handler() {
            override fun handleMessage(msg: Message) {
                when (msg.what) {
                    MSG_ANIM -> {
                        mCurrFrame++
                        mCurrDegree += mDeltaDegree
                        if (mCurrFrame > frameCount) {
                            mCurrFrame = 1
                        }
                        postInvalidate()
                    }
                }
            }
        }
    }

}
